package com.xzlf.controller;

import com.alibaba.fastjson.JSON;
import com.xzlf.config.R;
import com.xzlf.config.RSAProperties;
import com.xzlf.util.RSAUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


@RestController
@Slf4j
public class RSAController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private RSAProperties rsaProperties;

    @GetMapping("/getPublicKey")
    public String getPublicKey() throws Exception {
        return rsaProperties.getPublicKey();
    }

    @GetMapping("/getPrivateKey")
    public String getPrivateKey() throws Exception {
        return rsaProperties.getPrivateKey();
    }

    @GetMapping("/dec")
    public Object dec(){
        String url = "http://localhost:8081/user/list";
        ResponseEntity<R> exchange = restTemplate.exchange(url, HttpMethod.GET, null, R.class);
        R body = exchange.getBody();
        log.info("===加密结果:{}==========", body);
        String data = (String) body.getData();
        String s = RSAUtil.decryptDataOnJava(data, rsaProperties.getPrivateKey());
        body.setData(s);
        return body;
    }
}
